2

I am struggling to filter list in the body based on the selection of list of checkboxes under the protocol in the left see on jsfiddle please help.

filteredRecords: function(){
        return ko.utils.arrayFilter(viewModel.protocoldocs,function(protocoldoc){
           var flag = false;
    foreach(selprotocol in viewModel.selectedprotocol)
    {

        if(selprotocol.id === protocoldoc.pronumber)
        flag = true;     
    }
    return flag;
        })};

1 Answer 1

4

You could use a computed observable for this purpose like below

viewModel.filteredProtocols = ko.computed(function () {
  var selectedProtocols = ko.utils.arrayFilter(viewModel.protocol(), function (p) {
    return p.selected();
  });
  if (selectedProtocols.length == 0)   //if none selected return all
    return viewModel.protocoldocs();
  else { //other wise only return selected protocoldocs
    return ko.utils.arrayFilter(viewModel.protocoldocs(), function (item) {
      return ko.utils.arrayFilter(selectedProtocols, function (p) {
        return p.id == item.id
      }).length > 0;
    });
  }
})

and bind your result table to this filteredProtocol. A couple of things that i have also modified are

I added a selected flag for protocol to retain checked values

<input type="checkbox" data-bind="checked:selected, attr: {id: 'checkBox' + id}">

...

function protocol(id, name) {
  this.id = id;
  this.name = name;
  this.selected = ko.observable(false);
}

you can find a working sample here http://jsfiddle.net/prc4pqnm/3/

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you Sam. you save my day. that is what I was looking for. the only thing I had to change in compute function from return p.id == item.id to return p.name == item.pronumber so that it will filter based on protocol number instead of the id.
and I will add more filter check boxes under site , branches and so on and modify computed function you created to get the filtered result. let me know if this the right direction. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.